package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportSmsTemplateRpcService;
import com.hunttown.mes.rpc.domain.TransportSmsTemplateDTO;
import com.hunttown.mes.rpc.domain.query.TransportSmsTemplateDTOQuery;
import com.hunttown.mes.service.TransportSmsTemplateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportSmsTemplateConverts.convert;

/**
 * created by wangjunfu on 2019-05-19 01:20:16
 */
@Service
public class TransportSmsTemplateRpcServiceImpl implements TransportSmsTemplateRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportSmsTemplateRpcServiceImpl.class);

    private final TransportSmsTemplateService transportSmsTemplateService;

    @Autowired
    public TransportSmsTemplateRpcServiceImpl(TransportSmsTemplateService transportSmsTemplateService) {
        this.transportSmsTemplateService = transportSmsTemplateService;
    }

    @Override
    public TransportSmsTemplateDTO insert(TransportSmsTemplateDTO transportSmsTemplateDTO) {
        try {
            return convert(transportSmsTemplateService.insert(convert(transportSmsTemplateDTO)));
        } catch (Exception e) {
            logger.error("TransportSmsTemplate create failed.", e);
            throw new RuntimeException("TransportSmsTemplate create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportSmsTemplateDTO transportSmsTemplateDTO) {
        try {
            return transportSmsTemplateService.updateInfoById(convert(transportSmsTemplateDTO));
        } catch (Exception e) {
            logger.error("TransportSmsTemplate update by id failed.", e);
            throw new RuntimeException("TransportSmsTemplate update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportSmsTemplateDTOQuery transportSmsTemplateDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportSmsTemplateDTOQuery.getW_id() != null && transportSmsTemplateDTOQuery.getW_id() > 0) {
                query.put("w_id", transportSmsTemplateDTOQuery.getW_id());
            }

            if (transportSmsTemplateDTOQuery.getIds() != null && !transportSmsTemplateDTOQuery.getIds().equals("")) {
                query.put("ids", transportSmsTemplateDTOQuery.getIds());
            }

            if (transportSmsTemplateDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportSmsTemplateDTOQuery.getStopFlag());
            }

            if (transportSmsTemplateDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportSmsTemplateDTOQuery.getDeleteFlag());
            }

            return transportSmsTemplateService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportSmsTemplate update by query failed.", e);
            throw new RuntimeException("TransportSmsTemplate update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportSmsTemplateDTO selectById(Integer id) {
        try {
            return convert(transportSmsTemplateService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportSmsTemplate select by id failed.", e);
            throw new RuntimeException("TransportSmsTemplate select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportSmsTemplateDTO selectObjByQuery(TransportSmsTemplateDTOQuery transportSmsTemplateDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportSmsTemplateDTOQuery.getEntId() != null) {
                query.put("entId", transportSmsTemplateDTOQuery.getEntId());
            }

            if (transportSmsTemplateDTOQuery.getTemplateType() != null) {
                query.put("templateType", transportSmsTemplateDTOQuery.getTemplateType());
            }

            if (transportSmsTemplateDTOQuery.getDiscernCode() != null) {
                query.put("discernCode", transportSmsTemplateDTOQuery.getDiscernCode());
            }

            if (transportSmsTemplateDTOQuery.getSmsType() != null) {
                query.put("smsType", transportSmsTemplateDTOQuery.getSmsType());
            }

            if (transportSmsTemplateDTOQuery.getSmsTopic() != null) {
                query.put("smsTopic", transportSmsTemplateDTOQuery.getSmsTopic());
            }

            if (transportSmsTemplateDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportSmsTemplateDTOQuery.getStopFlag());
            }

            if (transportSmsTemplateDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportSmsTemplateDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(transportSmsTemplateDTOQuery.getOrderBy())) {
                query.put("orderBy", transportSmsTemplateDTOQuery.getOrderBy());
            }

            return convert(transportSmsTemplateService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportSmsTemplate select obj by query failed.", e);
            throw new RuntimeException("TransportSmsTemplate select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportSmsTemplateDTO> getListForPage(TransportSmsTemplateDTOQuery transportSmsTemplateDTOQuery) {
        Query query = Query.page(transportSmsTemplateDTOQuery.getPn(), transportSmsTemplateDTOQuery.getPageSize());

        if (transportSmsTemplateDTOQuery.getId() != null && transportSmsTemplateDTOQuery.getId() > 0) {
            query.put("id", transportSmsTemplateDTOQuery.getId());
        }

        if (transportSmsTemplateDTOQuery.getIds() != null && !transportSmsTemplateDTOQuery.getIds().equals("")) {
            query.put("ids", transportSmsTemplateDTOQuery.getIds());
        }

        if (transportSmsTemplateDTOQuery.getTemplateType() != null) {
            query.put("templateType", transportSmsTemplateDTOQuery.getTemplateType());
        }

        if (StringUtils.isNotBlank(transportSmsTemplateDTOQuery.getLikeName())) {
            query.put("likeName", transportSmsTemplateDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(transportSmsTemplateDTOQuery.getDiscernCode())) {
            query.put("discernCode", transportSmsTemplateDTOQuery.getDiscernCode());
        }

        if (StringUtils.isNotBlank(transportSmsTemplateDTOQuery.getShowDate())) {
            query.put("showDate", transportSmsTemplateDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportSmsTemplateDTOQuery.getStartDate())) {
            query.put("startDate", transportSmsTemplateDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportSmsTemplateDTOQuery.getEndDate())) {
            query.put("endDate", transportSmsTemplateDTOQuery.getEndDate());
        }

        if (transportSmsTemplateDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportSmsTemplateDTOQuery.getStopFlag());
        }

        if (transportSmsTemplateDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportSmsTemplateDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(transportSmsTemplateDTOQuery.getOrderBy())) {
            query.put("orderBy", transportSmsTemplateDTOQuery.getOrderBy());
        }

        List<TransportSmsTemplateDTO> list = convert(transportSmsTemplateService.selectByQuery(query));
        Page<TransportSmsTemplateDTO> page = Page.from(transportSmsTemplateDTOQuery.getPn(), transportSmsTemplateDTOQuery.getPageSize(), list);
        page.setTotalItems(transportSmsTemplateService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportSmsTemplateService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportSmsTemplate delete failed.", e);
            throw new RuntimeException("TransportSmsTemplate delete failed, Please order your code.", e);
        }
    }
}